from rest_framework import serializers

from Group.models import Team, TeamMemberShip
from Person.models import Person


class TeamCreatorSerializer(serializers.ModelSerializer):
    avatar = serializers.SerializerMethodField()

    class Meta:
        model = Person
        fields = (
            'id',
            'username',
            'avatar',
        )

    def get_avatar(self, instance):
        return instance.detail.profile_picture.url


class TeamListSerializer(serializers.ModelSerializer):
    memberCount = serializers.SerializerMethodField(read_only=True)
    creator = TeamCreatorSerializer(read_only=True)
    createTime = serializers.DateTimeField(source='create_time', format='%Y-%m-%d %H:%M:%S', read_only=True)
    teamDetail = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = Team
        fields = (
            'id',
            'teamDetail',
            'introduction',
            'name',
            'creator',
            'team_type',
            'memberCount',
            'createTime',
        )

    def get_memberCount(self, instance):
        return instance.total_members

    def get_teamDetail(self, instance):
        return instance.team_detail.id


class TeamMemberShipSerializer(serializers.ModelSerializer):
    class Meta:
        model = TeamMemberShip
        fields = ('team', 'person', 'name', 'is_active', 'is_admin', 'is_quit')
        read_only_fields = ('is_active', 'is_admin', 'is_quit')


class TeamMemberShipApprovalSerializer(serializers.ModelSerializer):
    class Meta:
        model = TeamMemberShip
        fields = ['is_active']


class TeamJoinRequestSerializer(serializers.ModelSerializer):
    class Meta:
        model = TeamMemberShip
        fields = ['id', 'person', 'name', 'create_time']
